package com.npjscg.medicalpro.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.npjscg.medicalpro.common.Constants;
import com.npjscg.medicalpro.common.Result;
import com.npjscg.medicalpro.config.AuthAccess;
import com.npjscg.medicalpro.controller.dto.BusinessDTO;
import com.npjscg.medicalpro.entity.Bcheck;
import com.npjscg.medicalpro.entity.Business;
import com.npjscg.medicalpro.service.IBcheckService;
import com.npjscg.medicalpro.service.IBusinessService;

import com.npjscg.medicalpro.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author raote
 * @since 2022-07-09
 */
@Api(tags = "事务管理模块")
@RestController
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private IBusinessService businessService;

    @Autowired
    private IUserService userService;


    @Autowired
    private IBcheckService bcheckService;

    @ApiOperation(value = "管理员获取所有上报事务,指定status")
    @GetMapping("/findAll/{pagenum}/{pagesize}/{status}")
    public Result findAll(@PathVariable int pagenum, @PathVariable int pagesize, @PathVariable int status){
        Page<BusinessDTO> page = new Page<>(pagenum,pagesize);
        return Result.success(businessService.findAll(page, status));
    }




    @ApiOperation(value = "用户获取三种状态的事务，前端用vuex传入当前用户id并指定状态 0 未审核 1 退回 2 通过")
    @GetMapping("/findAllForU/{pagenum}/{pagesize}/{id}/{status}")
    public Result findAllForU(@PathVariable int pagenum, @PathVariable int pagesize,@PathVariable int id, @PathVariable int status){
        Page<Business> page = new Page<>(pagenum,pagesize);
        return Result.success(businessService.findAllForU(page,id, status));
    }

    @ApiOperation(value = "查看事务具体内容")
    @GetMapping("/checkBusiness/{id}")
    public Result checkBusiness(@PathVariable int id){
        Business byId = businessService.getById(id);
        if (byId == null)
            return Result.error(Constants.CODE_600, "数据库中没有该数据信息");
        else
            return Result.success(byId);
    }


    @ApiOperation(value = "新增事务")
    @PostMapping("/newBusiness")
    public Result newBusiness(@RequestBody Business business){
        if (userService.getById(business.getUid()) == null){
            return Result.error(Constants.CODE_600, "数据中没有此用户信息出错");
        } else if (StrUtil.isBlank(business.getTitle()) || StrUtil.isBlank(business.getContents())) {
            return Result.error("-1", "事务标题与内容不能为空！");
        }
            else
        {
            businessService.saveBusiness(business);
        }
        return Result.success();
    }

    @ApiOperation(value = "删除事务,输入事务的的id")
    @DeleteMapping("/delete/{id}")
    public Result deleteBusiness(@PathVariable int id){
        Business byId = businessService.getById(id);
        if (byId == null)
            return Result.error(Constants.CODE_600, "数据库中没有该记录！");
        else
            businessService.removeById(byId.getId());
        return Result.success();
    }

    @ApiOperation(value = "编辑事务，输入事务的id")
    @PostMapping("/edit")
    public Result edit(@RequestBody Business business){
        Business byId = businessService.getById(business.getId());
        if (byId == null)
            return Result.error(Constants.CODE_600, "数据库中没有该记录！");
        else{
            UpdateWrapper<Bcheck> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("bid", business.getId());
            updateWrapper.set("msg","已重新编辑，请管理员重新审核");
            updateWrapper.set("status",0);
            bcheckService.update(updateWrapper);
            businessService.updateById(business);
        }
        return Result.success();
    }


}
